import { getStore, setStore } from '@/util/store';

import { getBomDetailMenuTree } from '@/api/admin/bom';
import { getDetail, processProductRouteByWorkorder } from '@/api/product/productrouteManage';

const designer = {
  namespaced: true,
  state: {
    designer: null, // gojs 设计器
    currentStationInfo: {}, // 当前选中站点信息
    allActiveStations: [] // 编辑区域所有非开始结束节点的集合
  },
  actions: {
    // 根据工艺路线ID,获取工艺详情
    async getProcessDetail({ commmit }, payload) {
      try {
        const res = await getDetail(payload);
        return res.data.data;
      } catch (error) {
        throw error;
      }
    },
    // 根据工单号，查询工艺路线
    async getProcessWorkRouteByWo({ commit }, payload) {
      try {
        const res = await processProductRouteByWorkorder(payload);
        return res.data.data;
      } catch (error) {
        throw error;
      }
    },
    // 根据bomid查询Bom半成品列表
    async getBomTreeDetail({ commit }, payload) {
      try {
        const res = await getBomDetailMenuTree(payload);
        return res.data.data;
      } catch (error) {
        throw error;
      }
    }
  },
  mutations: {
    // 设置当前工艺站点
    setCurrentStationInfo: (state, data) => {
      state.currentStationInfo = data;
    },
    // 设置所有有效工艺站点
    setAllActiveStations: (state, data) => {
      state.allActiveStations.push(data);
    },
    // 一次全部设置所有有效工艺站点
    singleUseSetAllActiveStations: (state, data) => {
      state.allActiveStations = data;
    },
    changeAllActiveStations: (state, singStation) => {
      const index = state.allActiveStations.findIndex((station) => station.key == singStation.key);
      if (index > -1) {
        state.allActiveStations.splice(index, 1, singStation);
      }
    },
    removeAllActiveStations: (state, singStation) => {
      const index = state.allActiveStations.findIndex((station) => station.key == singStation.key);
      if (index > -1) {
        state.allActiveStations.splice(index, 1);
      }
    }
  }
};

export default designer;
